package com.mkyong.android;

import android.app.Dialog;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;

public class MainActivity extends TabActivity implements OnTabChangeListener {
	private TabHost tabHost;
	private int oldId = 0;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		Resources ressources = getResources();
		tabHost = getTabHost();

		// Home tab
		Intent intentHome = new Intent().setClass(this, HomeActivity.class);
		TabSpec tabSpecHome = tabHost
				.newTabSpec("Home")
				.setIndicator("",
						ressources.getDrawable(R.drawable.icon_home_config))
				.setContent(intentHome);

		// Book tab
		Intent intentBook = new Intent().setClass(this, EditActivity.class);
		TabSpec tabSpecBook = tabHost
				.newTabSpec("Book")
				.setIndicator("",
						ressources.getDrawable(R.drawable.icon_books_config))
				.setContent(intentBook);

		// help tab
		Intent intentHelp = new Intent().setClass(this, HelpActivity.class);
		TabSpec tabSpecHelp = tabHost
				.newTabSpec("help")
				.setIndicator("",
						ressources.getDrawable(R.drawable.icon_help_config))
				.setContent(intentHelp);

		// store tab
		Intent intentStore = new Intent().setClass(this, StoreActivity.class);
		TabSpec tabSpecStore = tabHost
				.newTabSpec("store")
				.setIndicator("",
						ressources.getDrawable(R.drawable.icon_store_config))
				.setContent(intentStore);

		// add all tabs
		tabHost.addTab(tabSpecHome);
		tabHost.addTab(tabSpecBook);
		tabHost.addTab(tabSpecHelp);
		tabHost.addTab(tabSpecStore);

		// set Windows tab as default (zero based)
		tabHost.setCurrentTab(0);

		tabHost.setOnTabChangedListener(this);

	}

	public void onTabChanged(String tabId) {

		int i = tabHost.getCurrentTab();

		if ("store".equals(tabId)) {
			tabHost.setCurrentTab(oldId);
			showDialog();
		} else {
			oldId = i;
		}

		return;
	}

	private void showDialog() {
		// custom dialog
		final Dialog dialog = new Dialog(this);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.setContentView(R.layout.custom);

		Button dialogButton = (Button) dialog.findViewById(R.id.btnOk);
		Button btnSatu = (Button) dialog.findViewById(R.id.btnSatu);
		// if button is clicked, close the custom dialog
		btnSatu.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				
				//TODO CONDITON FOR DATE BEFORE 25 JULY AND AFTER 25 JULY
				
				Intent browse = new Intent(Intent.ACTION_VIEW, Uri.parse("http://rcs.music.jp/book/"));
				startActivity(browse);
			}
		});
		Button btnDua = (Button) dialog.findViewById(R.id.btnDua);
		// if button is clicked, close the custom dialog
		btnDua.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent browse = new Intent(Intent.ACTION_VIEW, Uri.parse("http://rcyomel.mopita.com/yomel-store/"));
				startActivity(browse);
			}
		});
		// if button is clicked, close the custom dialog
		dialogButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.dismiss();
			}
		});

		dialog.show();
	}

}